<?php
/**
 * 部门编辑
 * @author Dracowyn
 * @since 2023-11-17 17:37
 */

// 引入配置文件
include_once('config/config.php');

$loginAuth = check_auth();

$id = $_GET['id'] ?? 0;
$depSql = "SELECT * FROM pre_department WHERE id = $id";
$depData = find($depSql);
if (!$depData) {
	showMsg('部门不存在');
	exit();
}

if ($_POST) {
	$name = trim($_POST['name']);
	if (empty($name)) {
		showMsg('部门名称不能为空');
		exit();
	}
	$sql = "SELECT name FROM pre_department WHERE name = '$name' AND id != $id";
	$findName = all($sql);
	if ($findName) {
		showMsg('部门名称已存在');
		exit();
	}
	$data = [
		'name' => $name,
	];
	$res = update('department', $data, "id = $id");
	if ($res['code'] === 0) {
		showMsg($res['msg']);
	} else {
		showMsg('修改成功', 'depList.php');
	}
}

?>

<!DOCTYPE html>
<html lang="zh">
<head>
	<?php include_once('common/meta.php'); ?>
	<title>编辑部门</title>
</head>

<body>

<?php include_once('common/header.php'); ?>

<?php include_once('common/menu.php'); ?>

<div class="content">
	<div class="header">
		<h1 class="page-title">编辑部门</h1>
	</div>
	<ul class="breadcrumb">
		<li><a href="index.php">首页</a> <span class="divider">/</span></li>
		<li><a href="depList.php">部门管理</a> <span class="divider">/</span></li>
		<li class="active">编辑部门</li>
	</ul>

	<div class="container-fluid">
		<div class="row-fluid">

			<div class="btn-toolbar">
				<button class="btn btn-primary" onClick="location='depList.php'"><i class="icon-list"></i>部门列表
				</button>
				<div class="btn-group">
				</div>
			</div>

			<div class="well">
				<div id="myTabContent" class="tab-content">
					<div class="tab-pane active in" id="home">
						<form method="post" enctype="multipart/form-data">
							<label for="name">部门名称</label>
							<input type="text" name="name" id="name" class="input-xxlarge"
								   placeholder="请输入部门名称"
								   value="<?php echo $depData['name']; ?>">
							<label></label>
							<input class="btn btn-primary" type="submit" value="提交"/>
						</form>
					</div>
				</div>
			</div>
			<?php include_once('common/footer.php'); ?>
		</div>
	</div>
</div>
</body>
<?php include_once('common/script.php'); ?>
</html>
